# -*- mode: Makefile; -*-
# Copyright (c) 2011 Bitcoin Developers
# Distributed under the MIT/X11 software license, see the accompanying
# file COPYING or http://www.opensource.org/licenses/mit-license.php.

# Mac OS X makefile for ganjacoind
# Originally by Laszlo Hanyecz (solar@heliacal.net)

CXX=clang++
C=clang
DEPSDIR=/usr/local

INCLUDEPATHS= \
 -I"$(CURDIR)" \
 -I"$(CURDIR)"/obj \
 -I"$(DEPSDIR)/include" \
 -I"$(DEPSDIR)/Cellar/boost/1.59.0/include" \
 -I"$(DEPSDIR)/Cellar/berkeley-db4/4.8.30/include" \
 -I"$(DEPSDIR)/openssl-1.0.1p/include"

LIBPATHS= \
 -L"$(DEPSDIR)/lib" \
 -L"$(DEPSDIR)/Cellar/boost/1.59.0/lib" \
 -L"$(DEPSDIR)/Cellar/berkeley-db4/4.8.30/lib" \
 -L"$(DEPSDIR)/openssl-1.0.1p/lib"

USE_UPNP:=1
USE_WALLET:=1
USE_LOWMEM:=0

LIBS= -dead_strip

ifdef STATIC
# Build STATIC if you are redistributing the ganjacoind binary
LIBS += \
 $(DEPSDIR)/lib/db48/libdb_cxx-4.8.a \
 $(DEPSDIR)/lib/libboost_system.a \
 $(DEPSDIR)/lib/libboost_filesystem.a \
 $(DEPSDIR)/lib/libboost_program_options.a \
 $(DEPSDIR)/lib/libboost_thread-mt.a \
 $(DEPSDIR)/lib/libssl.a \
 $(DEPSDIR)/lib/libcrypto.a \
 -lz
else
LIBS += \
 -l db_cxx-4.8 \
 -l boost_system \
 -l boost_filesystem \
 -l boost_program_options \
 -l boost_thread-mt \
 -l ssl \
 -l crypto \
 -l z
endif

DEFS=-DMAC_OSX -DMSG_NOSIGNAL=0 -DBOOST_SPIRIT_THREADSAFE

ifdef RELEASE
# Compile for maximum compatibility and smallest size.
# This requires that dependencies are compiled
# the same way.
CFLAGS = -mmacosx-version-min=10.8 -arch x86_64 -O3
else
DEBUGFLAGS = -g
endif

# ppc doesn't work because we don't support big-endian
CFLAGS += -Wall -Wextra -Wformat -Wno-ignored-qualifiers -Wformat-security -Wno-unused-parameter -Wunused-function -Wunused-variable -fpermissive -Wconversion-null -Wno-deprecated-declarations\
    $(DEBUGFLAGS) $(DEFS) $(INCLUDEPATHS)

OBJS= \
    obj/alert.o \
    obj/allocators.o \
    obj/version.o \
    obj/support/cleanse.o \
    obj/checkpoints.o \
    obj/netbase.o \
    obj/addrman.o \
    obj/base58.o \
    obj/crypter.o \
    obj/key.o \
    obj/pubkey.o \
    obj/ecwrapper.o \
    obj/init.o \
    obj/bitcoind.o \
    obj/keystore.o \
    obj/core.o \
    obj/main.o \
    obj/net.o \
    obj/protocol.o \
    obj/rpcclient.o \
    obj/rpcprotocol.o \
    obj/rpcserver.o \
    obj/rpcmisc.o \
    obj/rpcnet.o \
    obj/rpcblockchain.o \
    obj/rpcrawtransaction.o \
    obj/rpcsmessage.o \
    obj/script.o \
    obj/scrypt.o \
    obj/sync.o \
    obj/txmempool.o \
    obj/util.o \
    obj/hash.o \
    obj/noui.o \
    obj/kernel.o \
    obj/pbkdf2.o \
    obj/chainparams.o \
    obj/stealth.o \
    obj/activemasternode.o \
    obj/darksend.o \
    obj/darksend-relay.o \
    obj/instantx.o \
    obj/masternodeconfig.o \
    obj/masternodeman.o \
    obj/masternode.o \
    obj/masternode-payments.o \
    obj/rpcdarksend.o \
    obj/spork.o \
    obj/crypto/hmac_sha256.o \
    obj/crypto/hmac_sha512.o \
    obj/crypto/ripemd160.o \
    obj/crypto/sha1.o \
    obj/crypto/sha256.o \
    obj/crypto/sha512.o \
    obj/smessage.o

ifeq (${USE_WALLET}, 1)
    DEFS += -DENABLE_WALLET
    OBJS += \
        obj/db.o \
        obj/miner.o \
        obj/rpcdump.o \
        obj/rpcmining.o \
        obj/rpcwallet.o \
        obj/wallet.o \
        obj/walletdb.o
endif

ifndef USE_UPNP
	override USE_UPNP = -
endif
ifneq (${USE_UPNP}, -)
	DEFS += -DUSE_UPNP=$(USE_UPNP)
ifdef STATIC
	LIBS += $(DEPSDIR)/lib/libminiupnpc.a
else
	LIBS += -lminiupnpc
endif
endif

ifeq (${USE_LOWMEM}, 1)
    DEFS += -DLOWMEM
endif

all: ganjacoind

# build secp256k1
DEFS += $(addprefix -I,$(CURDIR)/secp256k1/include)
secp256k1/src/libsecp256k1_la-secp256k1.o:
	@echo "Building Secp256k1 ..."; cd secp256k1; chmod 755 *; ./autogen.sh; ./configure --enable-module-recovery; make; cd ..;
ganjacoind: secp256k1/src/libsecp256k1_la-secp256k1.o

# build leveldb
LIBS += $(CURDIR)/leveldb/libleveldb.a $(CURDIR)/leveldb/libmemenv.a
DEFS += $(addprefix -I,$(CURDIR)/leveldb/include)
DEFS += $(addprefix -I,$(CURDIR)/leveldb/helpers)
OBJS += obj/txdb-leveldb.o
leveldb/libleveldb.a:
	@echo "Building LevelDB ..."; cd leveldb; chmod 755 *; make libleveldb.a libmemenv.a; cd ..
obj/txdb-leveldb.o: leveldb/libleveldb.a

# auto-generated dependencies:
-include obj/*.P

obj/%.o: %.cpp
	$(CXX) -c $(CFLAGS) -MMD -MF $(@:%.o=%.d) -o $@ $<
	@cp $(@:%.o=%.d) $(@:%.o=%.P); \
	  sed -e 's/#.*//' -e 's/^[^:]*: *//' -e 's/ *\\$$//' \
	      -e '/^$$/ d' -e 's/$$/ :/' < $(@:%.o=%.d) >> $(@:%.o=%.P); \
	  rm -f $(@:%.o=%.d)

obj/%.o: %.c
	$(C) -c $(CFLAGS) -fpermissive -MMD -MF $(@:%.o=%.d) -o $@ $<
	@cp $(@:%.o=%.d) $(@:%.o=%.P); \
	  sed -e 's/#.*//' -e 's/^[^:]*: *//' -e 's/ *\\$$//' \
	      -e '/^$$/ d' -e 's/$$/ :/' < $(@:%.o=%.d) >> $(@:%.o=%.P); \
	  rm -f $(@:%.o=%.d)

ganjacoind: $(OBJS:obj/%=obj/%)
	$(CXX) $(CFLAGS) -o $@ $(LIBPATHS) $^ $(LIBS)

clean:
	-rm -f ganjacoind
	-rm -f obj/*.o
	-rm -f obj/*.P
	-rm -f obj/build.h

FORCE:
